<?php
    class ConsejosController extends AppController {
        var $name = 'Consejos';
        var $uses = array('Consejo');
        var $helpers = array('Javascript');
        var $components = array('AdminParameters');

        function index(){

            $consejos = $this->__getListData();

            $this->set("consejos", $consejos);

            $this->render("index");
        }

        function  getList($page=0, $limit=10, $orderColumn='id', $ascDesc='DESC'){

            $consejos = $this->__getListData($this->params["form"], $page, $limit, $orderColumn, $ascDesc);

            $this->set("consejos", $consejos);

            $this->render("listado_consejos", "ajax");
        }

        function getListData($page=0, $limit=10, $orderColumn='id', $ascDesc='DESC'){
            $consejos = $this->__getListData($this->params["form"], $page, $limit, $orderColumn, $ascDesc);

            $this->set("result", $consejos);

            $this->render("/elements/ajax_return", "ajax");
        }

        function __getListData($params = array(), $page=0, $limit=10, $orderColumn='id', $ascDesc='DESC'){

            $model = "Consejo.";

            $fields = array(
                 $model."id", $model."titulo", $model."consejo"
            );

            $result = array();

            $result["data"] = $this->Consejo->find('all', array('order' => 'id DESC'));

            //$result = $this->AdminParameters->getListData($this->Consejo, $fields, $params, $page, $limit, $orderColumn, $ascDesc);

            return $result;

        }

        function getDetail($op, $id){

            $this->set("operation", $op);
            $this->set("editId", $id);

            $this->render("detalle_consejo", "ajax");

        }

        function getPdf($id){
            $data = $this->Consejo->find("all", array("conditions" => array("Consejo.id" => $id), "recursive" => 0));

            $this->layout = "pdf";

            $this->set("data", $data);
        }

        function getDetailData($id){
            $data = $this->Consejo->find("all", array("conditions" => array("Consejo.id" => $id), "recursive" => 0));

            $this->set("result", array("data" => $data));

            $this->render("/elements/ajax_return", "ajax");
        }

        function save(){

            if($this->AdminParameters->doSave($this->Consejo, $this->params["form"])){
                $this->set("result", array("result" => true));
            }else{
                $this->set("result", array("result" => false));
                $this->set("result", array("error" => "Error while saving data."));
            }


            $this->render("/elements/ajax_return", "ajax");
        }

        function download($id = null) {
            // Include Component
            App::import('Component', 'Pdf');
            // Make instance
            $Pdf = new PdfComponent();
            // Invoice name (output name)
            $Pdf->filename = 'your_invoice'; // Without .pdf
            // You can use download or browser here
            $Pdf->output = 'browser';
            $Pdf->init();
            // Render the view
            $Pdf->process(Router::url('/', true) . 'consejos/getPdf/'. $id);
            $this->render(false);
        }

        function getConsejo($id){
            $result = $this->Consejo->find('first', array(
                'order' => 'id DESC',
                'conditions' => array("Consejo.id" => $id),
                'fields' => array("Consejo.consejo")
            ));

            $this->set("result", $result);
            
            $this->render("get_consejo", "no_layout");
        }
    }
?>